package com.ruoyi.goods.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.vo.GoodsQueryVo;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.GoodsChoice;
import com.ruoyi.system.domain.vo.GoodsSearchVO;
import com.ruoyi.system.service.GoodsChoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsChoiceController {

    @Autowired
    private GoodsChoiceService goodsChoiceService;
    @Autowired
    private TokenService tokenService;

    /**
     * 用户获取商品列表
     *
     * @param current 页码
     */
    @PostMapping("/list/{current}")
    public AjaxResult getGoodsList(@PathVariable("current") int current,
                                   @RequestBody(required = false) GoodsQueryVo goodsQueryVo) {
        PageHelper.startPage(current, 10);
        List<GoodsChoice> goodsChoices = goodsChoiceService.queryAllByLimit(goodsQueryVo);
        return AjaxResult.success(PageInfo.of(goodsChoices));
    }

    /**
     * 商家获取商品列表
     * <p>
     * 这个需要传递一个
     *
     * @param current 页码
     */
    @PostMapping("/admin/list/{current}")
    public AjaxResult getGoodsListAdmin(@PathVariable("current") int current,
                                        @RequestBody(required = false) GoodsQueryVo goodsQueryVo) {
        PageHelper.startPage(current, 10);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (goodsQueryVo == null) goodsQueryVo = new GoodsQueryVo();
        goodsQueryVo.setUserId(loginUser.getUser().getUserId().toString());
        List<GoodsChoice> goodsChoices = goodsChoiceService.queryAllByLimit(goodsQueryVo);
        return AjaxResult.success(PageInfo.of(goodsChoices));
    }

    @PostMapping("/getGoodsListByUserId/{current}")
    public AjaxResult getGoodsListByUserId(@PathVariable("current") int current,
                                           @RequestBody(required = false) GoodsQueryVo goodsQueryVo) {
        PageHelper.startPage(current, 10);
        if (goodsQueryVo == null) goodsQueryVo = new GoodsQueryVo();
        List<GoodsChoice> goodsChoices = goodsChoiceService.queryAllByLimit(goodsQueryVo);
        return AjaxResult.success(PageInfo.of(goodsChoices));
    }

    /**
     * 更新
     *
     * @param goodsChoice 更新数据
     */
    @PostMapping("/update")
    public AjaxResult updateGoods(@RequestBody GoodsChoice goodsChoice) {
        goodsChoiceService.update(goodsChoice);
        return AjaxResult.success();
    }

    /**
     * 添加商品
     *
     * @param goodsChoice 添加数据
     */
    @PostMapping("/save")
    public AjaxResult saveGoods(@RequestBody GoodsChoice goodsChoice) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        goodsChoice.setUserId(loginUser.getUser().getUserId().toString());
        goodsChoice.setShopPhone(loginUser.getUser().getPhonenumber());
        GoodsChoice insert = goodsChoiceService.insert(goodsChoice);
        if (insert == null) return AjaxResult.error("分类不可以为空是空");
        return AjaxResult.success();
    }

    /**
     * 根据ID删除数据
     *
     * @param id id
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult deleteGoods(@PathVariable("id") int id) {
        goodsChoiceService.deleteById(id);
        return AjaxResult.success();
    }

    /***
     * 根据id查询商品数据
     * @param id
     * @return
     */
    @GetMapping("/getGoodsChoiceById/{id}")
    public AjaxResult getGoodsChoiceById(@PathVariable("id") int id) {
        GoodsChoice goodsChoice = goodsChoiceService.getGoodsChoiceById(id);
        return AjaxResult.success(goodsChoice);
    }

    @PostMapping("/getGoodsAll/{current}")
    public AjaxResult getGoodsAll(@PathVariable("current") int current,
                                  @RequestBody GoodsSearchVO goodsSearchVO) {
        PageHelper.startPage(current, 20);
        List<GoodsChoice> list = goodsChoiceService.getGoodsAll(goodsSearchVO);
        return AjaxResult.success(PageInfo.of(list));
    }

    @PostMapping("/editGoodsUrl")
    public AjaxResult editGoodsUrl(@RequestBody GoodsChoice goodsChoice) {
        Integer count = goodsChoiceService.editGoodsUrl(goodsChoice);
        return AjaxResult.success(count);
    }

    @GetMapping("/getGoodsUrlById/{id}")
    public AjaxResult getGoodsUrlById(@PathVariable("id") Integer id) {
        String goodsUrl = goodsChoiceService.getGoodsUrlById(id);
        return AjaxResult.success(goodsUrl);
    }

    @PostMapping("/addGood")
    public AjaxResult addGoods(@RequestBody GoodsChoice goodsChoice) {
        Integer count = goodsChoiceService.addGoods(goodsChoice);
        return AjaxResult.success(count);
    }

    @PostMapping("/top")
    public AjaxResult top(@RequestBody GoodsChoice goodsChoice) {
        Integer count = goodsChoiceService.top(goodsChoice);
        return AjaxResult.success(count);
    }

    @PostMapping("/delTop")
    public AjaxResult delTop(@RequestBody GoodsChoice goodsChoice) {
        Integer count = goodsChoiceService.delTop(goodsChoice);
        return AjaxResult.success(count);
    }
}
